<template>
  <el-dialog :visible.sync="value" :before-close="cancel">
    <template slot="title">
      {{auditStep===1?'一次审核':'二次审核'}}
    </template>
    <el-form :model="form" :rules="auditRule" ref="auditForm" :label-width="formLabelWidth">
      <el-form-item label="调整类型" >
        <el-col :span="16">
          <el-input @keyup.enter.native="toSave" :value="rowData.changeType | toConverter('ADJUSTMENT_TYPES_DETAIL')" disabled="disabled" auto-complete="off"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="调整金额" >
        <el-col :span="16">
          <el-input @keyup.enter.native="toSave" v-model="rowData.amount" disabled="disabled" auto-complete="off"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="账户名">
        <el-col :span="16">
          <el-input @keyup.enter.native="toSave" v-model="rowData.username" disabled="disabled" auto-complete="off"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="申请原由">
        <el-col :span="16">
          <el-input @keyup.enter.native="toSave" type="textarea" v-model="rowData.remark" disabled="disabled" auto-complete="off" ></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="一审备注" prop="remark" v-if="auditStep===1 && current === 0">
        <el-col :span="16">
          <el-input @keyup.enter.native="toSave" type="textarea" v-model="form.remark" auto-complete="off"/>
        </el-col>
      </el-form-item>
      <el-form-item label="一审备注" v-else>
        <el-col :span="16">
          <el-input @keyup.enter.native="toSave" type="textarea" v-model="rowData.firstRemark" disabled auto-complete="off"/>
        </el-col>
      </el-form-item>
      <el-form-item label="二审备注" v-if="auditStep===2 && current === 0" prop="remark">
        <el-col :span="16">
          <el-input @keyup.enter.native="toSave" type="textarea" v-model="form.remark" auto-complete="off"/>
        </el-col>
      </el-form-item>
      <el-form-item label="二审备注" v-if="auditStep===2 && current === 1" prop="secondRemark">
        <el-col :span="16">
          <el-input @keyup.enter.native="toSave" type="textarea" v-model="rowData.secondRemark" disabled auto-complete="off"/>
        </el-col>
      </el-form-item>
      <el-form-item label="审核结果" v-if="current === 0" prop="status">
        <el-col :span="16">
          <el-radio-group v-model="form.status" size="medium">
            <el-radio-button :label="1" value="1">通过</el-radio-button>
            <el-radio-button :label="2" value="2">不通过</el-radio-button>
          </el-radio-group>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" v-if="current === 0">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="toSave" :loading="!istap">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

  import {form} from '@/build'
  import {updateAdjustment} from '@/services/api/adjustment';

  export default {
    name: "adjustment-audit-dialog",
    props: ['value', 'id', 'auditStep','rowData', 'menuId'],
    mixins: [form],
    data() {
      return {
        formLabelWidth: '180px',
        istap:true,
        form: {
          remark: undefined,
          status: undefined,
          orderId: this.rowData.orderId,
          step:this.auditStep,
          menuId: this.menuId,
        },
        auditRule: {
          remark: [
            {required: true, message: '备注不能为空', trigger: 'blur'},
            { max: 45, message: '不能超过45个字符' },
          ],
          status: [
            {required: true, message: '请确认审核结果', trigger: 'blur'},
          ],
        }
      }
    },
    computed: {
      current() {
        let _current = 0;
        if (this.auditStep == 1) {
          _current = this.rowData.status === 0 ? 0: 1
        }
        if(this.auditStep == 2){
          _current = this.rowData.status === 1 ? 0:1
        }
        return _current
      },
    },

    watch:{
      auditStep(){
        this.form.step = this.auditStep
      }
    },
    mounted() {

      this.bindSave(updateAdjustment, 'saveForm', 'form', {
        beforeSend: (data) => {
          this.istap=false;
          data.step = this.auditStep
          return data
        },
        afterSuccess: () => {
          this.istap=true;
          this.cancel()
          this.$parent.getAll()
        },
        afterFail:()=>{
          this.istap=true;
        }
      });
    },
    methods: {
       toSave(){
        if(!this.istap){return;}
        // this.istap=false;
        this.doSave({action: 'saveForm', form: 'auditForm'})
      },
      cancel() {
        this.$emit('input', false);
      },
    },

  }
</script>

<style lang="scss" scoped>

</style>
